from fastapi import APIRouter,Depends,Request,HTTPException, status, Query
from novelapp.config.database import connect
from novelapp.models.models import Books,Comments
from pydantic import BaseModel
from novelapp.common.common import returnData,setLog
import json,httpx,time
from typing import Optional,List, Dict, Any
from tqdm import tqdm
import os
from datetime import datetime

logger = setLog('noveapp.log')
router = APIRouter()  
# 15730 * 20

EXPORT_BATCH_SIZE = 500  # 每批导出数量，可根据实际内存调整
EXPORT_OUTPUT_PATH = os.path.join(os.getcwd(), 'output')
EXPORT_MD_FILE = os.path.join(EXPORT_OUTPUT_PATH, 'books_knowledge.md')

def ensure_output_dir():
    if not os.path.exists(EXPORT_OUTPUT_PATH):
        os.makedirs(EXPORT_OUTPUT_PATH)

def book_to_markdown(book: Books, comments: List[Comments]) -> str:
    """
    将单本书及其评论转为Markdown格式，包含YAML元数据。
    """
    # 书籍元数据
    meta = {
        'title': book.title or '',
        'author': book.author or '',
        'score': book.score or 0,
        'scorerCount': book.scorerCount or 0,
        'tags': book.tags or [],
        'countWord': book.countWord or 0,
        'updateAt': book.updateAt.strftime('%Y-%m-%d') if book.updateAt else '',
        'intro': book.intro or '',
        'link': book.link or '',
        'cover': book.cover or '',
        'ysbookId': book.ysbookId or 0,
        'status': book.status or 0,
        'caseId': book.caseId or 0,
    }
    yaml_lines = ['---']
    for k, v in meta.items():
        if isinstance(v, list):
            yaml_lines.append(f'{k}: [{", ".join(map(str, v))}]')
        else:
            yaml_lines.append(f'{k}: "{v}"')
    yaml_lines.append('---')
    # 正文
    md = '\n'.join(yaml_lines)
    md += f"\n\n# {book.title or ''}\n\n"
    md += f"> **简介：** {book.intro or ''}\n\n"
    # 评论区
    if comments:
        md += '## 评论\n\n'
        for c in comments:
            md += f"- **{c.username}** ({c.createdAt.strftime('%Y-%m-%d %H:%M:%S') if c.createdAt else ''}) "
            md += f"评分: {c.rating} 赞: {c.upvotes}\n"
            md += f"  > {c.content}\n\n"
    else:
        md += '（暂无评论）\n'
    md += '\n---\n\n'
    return md

@router.get('/export_books_md', summary='导出所有书籍及评论为Markdown知识库')
def export_books_md() -> Dict[str, Any]:
    """
    分批导出所有书籍及其评论为Markdown知识库，生成output/books_knowledge.md。
    """
    ensure_output_dir()
    total = Books.objects.count()
    batch_size = EXPORT_BATCH_SIZE
    exported = 0
    with open(EXPORT_MD_FILE, 'w', encoding='utf-8') as f:
        for skip in tqdm(range(0, total, batch_size), desc='导出书籍'):
            books = Books.objects.skip(skip).limit(batch_size)
            for book in books:
                # 查询该书的所有评论
                comments = list(Comments.objects(book_id=book.id))
                md = book_to_markdown(book, comments)
                f.write(md)
                exported += 1
    return {"code": 200, "msg": f"导出完成，共导出{exported}本书，文件路径: {EXPORT_MD_FILE}", "data": {"count": exported, "file": EXPORT_MD_FILE}}